package nl.haemers.geom
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import nl.haemers.base.BaseSprite;
	import nl.haemers.interfaces.ICell;
	import nl.haemers.geom.BaseCell;
	
	import nl.haemers.utils.Logger;
	
	/**
	 * @author Jonathan Haemers
	*/
	public class BaseGrid extends BaseSprite
	{
		protected var _totalColumns				:int;
		protected var _totalRows 				:int;
		protected var _cellHeight				:Number;
		protected var _cellWidth				:Number;		
		protected var _strokeGrid				:Boolean;
		
		protected var gHeight					:Number;
		protected var gWidth					:Number;
		
		protected var _gridCells				:Vector.<ICell>;
		protected var _CustomCell				:Class;
		
		public function BaseGrid() 
		{
			super();
		}
		
		/** Public methods **/
		public function draw() : void
		{
			if (checkRequirements())
			{
				drawGrid();
			}
		}
		
		/** Getters & Setters **/
		public function get totalColumns() : int
		{ 
			return _totalColumns;
		}
		public function set totalColumns( value:int ) : void 
		{
			_totalColumns = value;
		}
		
		public function get totalRows() : int
		{ 
			return _totalRows;
		}
		public function set totalRows( value:int ) : void 
		{
			_totalRows = value;
		}
		
		public function get strokeGrid() : Boolean
		{ 
			return _strokeGrid;
		}
		public function set strokeGrid( value:Boolean ) : void 
		{
			_strokeGrid = value;
		}
		
		public function get cellWidth() : Number
		{ 
			return _cellWidth;
		}
		public function set cellWidth( value:Number ) : void 
		{
			_cellWidth = value;
		}
		
		public function get cellHeight() : Number
		{ 
			return _cellHeight;
		}
		public function set cellHeight( value:Number ) : void 
		{
			_cellHeight = value;
		}
		
		public function set customCell( value:Class ) : void 
		{
			//_customCell = new value() as BaseCell;
			_CustomCell = value;
		}
		
		/** Protected methods **/
		override protected function init(e:Event = null) : void
		{
			super.init(e);
			startup();
		}
		
		protected function startup() : void
		{
			
		}
		
		protected function drawGrid() : void
		{
			var cell:BaseCell;
			
			var gridCounter:int = 0;
			
			_gridCells = new Vector.<ICell>();
			//Need to build a solution for vertical grids :)
			
			for (var i:int = 0; i < _totalColumns; i++)
			{
				for (var j:int = 0; j < _totalRows; j++)
				{
					cell = (_CustomCell) ? new _CustomCell() : new BaseCell();
					if (!_CustomCell)
					{
						cell.cellWidth = _cellWidth;
						cell.cellHeight = _cellHeight;
						cell.x = i * cell.cellWidth;
						cell.y = j * cell.cellHeight;
						cell.lineColor = 0x000000;
						cell.backgroundColor = 0x00FF00;
					}
					
					cell.index = gridCounter;
					cell.cellLocation = new Point(i, j);
					
					cell.draw();
					addChild(cell);
					
					_gridCells[gridCounter] = cell;
					gridCounter++;
				}
			}
		}
		
		protected function checkRequirements() : Boolean
		{
			var valid:Boolean = true;
			
			return valid;
		}
	}

}